package com.ruoyi.service.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.service.entity.item.*;
import com.ruoyi.service.service.impl.PcItemService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * PC护理项目控制
 *
 * @author ZDL
 */

@RestController
@Api(tags = "PC护理项目")
@RequestMapping("/PC/userControl")
public class PcItemController extends BaseController {

    @Autowired
    private PcItemService pcItemService;

    /**
     * 获取护理项目列表方法
     *
     * @param
     * @return 结果
     */

    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @GetMapping("/getItemList")
    public AjaxResult getItemList(String keywords, String status) {
        try {
            List<PCCategoryVO> itemList = pcItemService.getItemList(keywords, status);
            return AjaxResult.success("获取成功", itemList);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 获取护理项目详情方法
     *
     * @param
     * @return 结果
     */

    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @GetMapping("/getItemDetail")
    public AjaxResult getItemDetail(Long itemId) {
        try {
            PCItemVO itemDetail = pcItemService.getItemDetail(itemId);
            return AjaxResult.success("获取成功", itemDetail);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 护理项目新增分类方法
     *
     * @param
     * @return 结果
     */

    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/addCategory")
    public AjaxResult addCategory(@RequestBody Map<String, Object> reqBody) {
        try {

            String categoryName = reqBody.get("categoryName").toString();
            Long linkId = Long.parseLong(reqBody.get("linkId").toString());

            Integer result = pcItemService.addCategory(categoryName, linkId);
            if (result == 1){
                return AjaxResult.success("护理分类添加成功");
            }else {
                return AjaxResult.error("护理分类添加失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 护理项目删除分类方法
     *
     * @param
     * @return 结果
     */

    //@PreAuthorize("@ss.hasPermi('service:items:list')")
    //@PostMapping("/delCategory")
    //public AjaxResult delCategory(@RequestBody Map<String, Object> reqBody) {
    //    try {
    //        Long categoryId = Long.parseLong(reqBody.get("categoryId").toString());
    //        Integer result = pcItemService.delCategory(categoryId);
    //        if (result == 0){
    //            return AjaxResult.success("护理项目删除分类成功");
    //        }else {
    //            return AjaxResult.error("护理项目删除分类失败");
    //        }
    //    }catch (Exception e){
    //        return AjaxResult.error(e.getMessage());
    //    }
    //
    //}

    /**
     * 护理项目新增分类下项目方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/addItem")
    public AjaxResult addItem(@RequestBody Map<String, Object> reqBody) {
        try {

            String itemName = reqBody.get("categoryName").toString();
            Long categoryId = Long.parseLong(reqBody.get("categoryId").toString());

            Integer result = pcItemService.addItem(itemName, categoryId);
            if (result == 1){
                return AjaxResult.success("护理分类添加成功");
            }else {
                return AjaxResult.error("护理分类添加失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }



    /**
     * 护理项目删除方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/delItem")
    public AjaxResult delItem(@RequestBody Map<String, Object> reqBody) {
        try {

            Long itemId = Long.parseLong(reqBody.get("itemId").toString());

            Integer result = pcItemService.delItem(itemId);
            if (result == 1){
                return AjaxResult.success("护理项目删除成功");
            }else {
                return AjaxResult.error("护理项目删除失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 护理项目编辑方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/updateItem")
    public AjaxResult updateItem(@RequestBody Map<String, Object> reqBody) {
        try {

            String itemName = reqBody.get("categoryName").toString();
            String desc = reqBody.get("desc").toString();
            String tip = reqBody.get("tip").toString();
            String pic = reqBody.get("pic").toString();
            Long itemId = Long.parseLong(reqBody.get("itemId").toString());

            Integer result = pcItemService.updateItem(itemName, desc, tip, pic, itemId);
            if (result == 1){
                return AjaxResult.success("护理项目修改成功");
            }else {
                return AjaxResult.error("护理项目修改失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 护理项目启用方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/yesItem")
    public AjaxResult yesItem(@RequestBody Map<String, Object> reqBody) {
        try {

            Long itemId = Long.parseLong(reqBody.get("itemId").toString());

            Integer result = pcItemService.yesItem(itemId);
            if (result == 1){
                return AjaxResult.success("护理项目启用成功");
            } else if (result == 2) {
                return AjaxResult.success("护理项目现已启用");
            } else {
                return AjaxResult.error("护理项目启用失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 护理项目停用方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/noItem")
    public AjaxResult noItem(@RequestBody Map<String, Object> reqBody) {
        try {

            Long itemId = Long.parseLong(reqBody.get("itemId").toString());

            Integer result = pcItemService.noItem(itemId);
            if (result == 1){
                return AjaxResult.success("护理项目停用成功");
            } else if (result == 2) {
                return AjaxResult.success("护理项目现已停用");
            } else {
                return AjaxResult.error("护理项目停用失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 护理项目分类编辑方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/updateCategory")
    public AjaxResult updateCategory(@RequestBody Map<String, Object> reqBody) {
        try {

            String categoryName = reqBody.get("categoryName").toString();
            Long categoryId = Long.parseLong(reqBody.get("categoryId").toString());
            Long linkId = Long.parseLong(reqBody.get("linkId").toString());

            Integer result = pcItemService.updateCategory(categoryId, categoryName, linkId);
            if (result == 1){
                return AjaxResult.success("护理分类编辑成功");
            }else {
                return AjaxResult.error("护理分类编辑失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 护理项目分类删除方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/delCategory")
    public AjaxResult delCategory(@RequestBody Map<String, Object> reqBody) {
        try {
            Long categoryId = Long.parseLong(reqBody.get("categoryId").toString());
            Integer result = pcItemService.delCategory(categoryId);
            if (result == 1){
                return AjaxResult.success("护理项目删除分类成功");
            }else {
                return AjaxResult.error("护理项目删除分类失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 护理项目分类启用方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/yesCategory")
    public AjaxResult yesCategory(@RequestBody Map<String, Object> reqBody) {
        try {
            Long categoryId = Long.parseLong(reqBody.get("categoryId").toString());
            Integer result = pcItemService.yesCategory(categoryId);
            if (result == 1){
                return AjaxResult.success("护理项目分类启用成功");
            }else {
                return AjaxResult.error("护理项目分类启用失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 护理项目分类停用方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/noCategory")
    public AjaxResult noCategory(@RequestBody Map<String, Object> reqBody) {
        try {
            Long categoryId = Long.parseLong(reqBody.get("categoryId").toString());
            Integer result = pcItemService.noCategory(categoryId);
            if (result == 1){
                return AjaxResult.success("护理项目分类停用成功");
            }else {
                return AjaxResult.error("护理项目分类停用失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 护理服务套餐耗材包查询方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @GetMapping("/packagesList")
    public AjaxResult packagesList() {
        try {

            List<PCPackagesList> pcPackagesLists = pcItemService.packagesList();
            return AjaxResult.success(pcPackagesLists);

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 护理服务套餐保存方法
     *
     * @param reqBody 信息
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/saveCombo")
    public AjaxResult saveCombo(@RequestBody PCComboReq reqBody) {
        try {
            Integer result = pcItemService.saveCombo(reqBody);
            if (result == 1){
                return AjaxResult.success("保存成功");
            }else {
                return AjaxResult.error("保存失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 护理服务套餐删除方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/delCombo")
    public AjaxResult delCombo(@RequestBody Map<String, Object> reqBody) {
        try {
            Long itemId = Long.parseLong(reqBody.get("itemId").toString());
            Long comboId = Long.parseLong(reqBody.get("comboId").toString());

            Integer result = pcItemService.delCombo(itemId, comboId);
            if (result == 1){
                return AjaxResult.success("删除成功");
            }else {
                return AjaxResult.error("删除失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 护理套餐详情查询方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @GetMapping("/comboDetail")
    public AjaxResult comboDetail(Long comboId) {
        try {
            //Long itemId = Long.parseLong(reqBody.get("itemId").toString());
            //Long comboId = Long.parseLong(reqBody.get("comboId").toString());

            PCComboRsp pcComboRsp = pcItemService.comboDetail(comboId);
            return AjaxResult.success("查询成功", pcComboRsp);

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 护理服务套餐关联耗材包删除方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:items:list')")
    @PostMapping("/delPackages")
    public AjaxResult delPackages(@RequestBody Map<String, Object> reqBody) {
        try {
            Long pckId = Long.parseLong(reqBody.get("pckId").toString());
            Long comboId = Long.parseLong(reqBody.get("comboId").toString());

            Integer result = pcItemService.delPackages(pckId, comboId);
            if (result == 1){
                return AjaxResult.success("删除成功");
            }else {
                return AjaxResult.error("删除失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


}
